package com.eclipse.e20200201_多线程同步.com.qf.app1.m3;

import java.lang.Thread.UncaughtExceptionHandler;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import com.eclipse.e20200201_多线程同步.com.qf.app1.JDBCUtils;

public class Test1 {

	public static void main(String[] args) {

		ThreadSe lines = new ThreadSe();

		ThreadSe lines2 = new ThreadSe();

		Task1 task1 = new Task1(lines);
		task1.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread t, Throwable e) {
				System.out.println(t.getName() + "出现了异常：" + e.getClass());
			}
		});
		task1.start();

		Task2 task2 = new Task2(lines);
		task2.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			@Override
			public void uncaughtException(Thread t, Throwable e) {
				System.out.println(t.getName() + "出现了异常：" + e.getClass());
			}
		});
		task2.start();

		try {
			TimeUnit.SECONDS.sleep(5);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		/*
		 * for (String temp : lines) { System.out.println(temp); }
		 */

	}

}

class ThreadSe {
	private static List<String> lines = new ArrayList<>();

	public static synchronized void add(String line) {
		lines.add(line);
	}

	public static synchronized void delete(String line) {
		lines.remove(line);
	}
	
	public static void print() {
		System.out.println("---------------");
	}

}

class Task2 extends Thread {
	private ThreadSe lines;

	public Task2(ThreadSe lines) {
		this.lines = lines;
	}

	public void run() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JDBCUtils.getConnection();

			statement = connection.prepareStatement("select * from score");

			resultSet = statement.executeQuery();
			String line = null;
			while (resultSet.next()) {
				line = resultSet.getInt("student_id") + "," + resultSet.getString("id");
				lines.add(line);
			}
		} catch (SQLException ex) {
			System.out.println("SQL 异常。。。。。。。。");
		} finally {
			JDBCUtils.closeResourec(connection, statement, resultSet);

		}
	}

}

class Task1 extends Thread {
	private ThreadSe lines;

	public Task1(ThreadSe lines) {
		this.lines = lines;
	}

	public void run() {
		Connection connection = null;
		PreparedStatement statement = null;
		ResultSet resultSet = null;

		try {
			connection = JDBCUtils.getConnection();

			statement = connection.prepareStatement("select * from student");

			resultSet = statement.executeQuery();
			String line = null;
			while (resultSet.next()) {
				line = resultSet.getInt("student_id") + "," + resultSet.getString("name");
				lines.add(line);
			}
		} catch (SQLException ex) {
			System.out.println("SQL 异常。。。。。。。。");
		} finally {
			JDBCUtils.closeResourec(connection, statement, resultSet);

		}
	}

}
